<template>
	<!-- 最外层的容器 -->
	<view class="my-settle-container">
		<!-- 全选区域 -->
		<label class="radio" @click="onisFullChecked">
			<radio color="#C00000" :checked="isFullChecked" /><text>全选</text>
		</label>

		<!-- 合计区域 -->
		<view class="amount-box">
			合计:<text class="amount">￥{{totalprice}}</text>
		</view>

		<!-- 结算按钮 -->
		<view class="btn-settle" @click="setAll">结算({{totals}})</view>
	</view>

</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		data() {
			return {}
		},
		methods: {
			...mapMutations('cart', ['updateAllGoods_state']),
			onisFullChecked() {
				this.updateAllGoods_state(!this.isFullChecked)
			},
			// 结算商品
			setAll() {
				if (!this.totals) return uni.showToast({
					title: '请选择结算商品'
				})
				if (!this.addressStr) return uni.showToast({
					title: '请选择收货地址'
				})
				// 未登录
				if (!this.token) return uni.showToast({
					title: '请登录'
				})
				this.payOrder()
				uni.showToast({
					title: '结算成功'
				})
			},
			async payOrder() {
				// 发送请求 创建订单
				// 发送请求 订单预支付
				// 调用微信api开始 支付
				const payObj = {
					order_price: 0.01,
					consignee_addr: this.addressStr,
					// 选中商品的列表
					goods: this.goods
				}
				const data = await uni.$http.post('/my/orders/create', payObj)
				if (data.data.meta.status !== 200) return uni.showToast({
					title: '订单创建失败'
				})
				// 获取订单编号
				``
				``
				``
				``
				``
				``
				``
				``
				``
				const order_number = data.message.order_number
				console.log(this.goods);
				// 发送请求 订单预支付
				const {
					data: res
				} = await uni.$http.post('/my/orders/req_unifiedorder', {
					order_number
				})
				if (res.meta.status !== 200) return uni.showToast({
					title: '订单预支付失败'
				})
				const payInfo = res.message.pay
				// 调用微信接口 api开始支付
				uni.requestPayment({
					provider: 'wxpay',
					...payInfo,
					success: (res) => {
						console.log('支付成功');
					},
					fail: (res) => {
						console.log('支付失败')
					},
				})
			}
		},
		computed: {
			...mapGetters('cart', ['totals', 'total', 'totalprice', 'goods']),
			...mapGetters('user', ['addressStr']),
			...mapState('user', ['token']),
			// 全选
			isFullChecked() {
				// 选中数量 是否等于全选数量
				return this.totals === this.total
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 50px;
		// 将背景色从 cyan 改为 white
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-left: 5px;
		font-size: 14px;
		z-index: 99999;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount {
			color: #c00000;
		}

		.btn-settle {
			height: 50px;
			min-width: 100px;
			background-color: #c00000;
			color: white;
			line-height: 50px;
			text-align: center;
			padding: 0 10px;
		}
	}
</style>
